package com.company.sensortest;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.widget.TextView;

public class SensorTest extends Activity implements SensorEventListener{
	
	SensorManager sensorManager = null;
	
	TextView accelerometerOuputX;
	TextView accelerometerOuputY;
	TextView accelerometerOuputZ;
		
	TextView orientationX;
	TextView orientationY;
	TextView orientationZ;
	
	GLSurfaceView mGlSurfaceView;
	
	CubeRenderer mCubeRenderer;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        setContentView(R.layout.main);
        
        accelerometerOuputX = (TextView) findViewById(R.id.accelerometer_x_value);
        accelerometerOuputY = (TextView) findViewById(R.id.accelerometer_y_value);
        accelerometerOuputZ = (TextView) findViewById(R.id.accelerometer_z_value);
        
        orientationX = (TextView) findViewById(R.id.orientation_x_value);
        orientationY = (TextView) findViewById(R.id.orientation_y_value);
        orientationZ = (TextView) findViewById(R.id.orientation_z_value);
        
        mGlSurfaceView = (GLSurfaceView) findViewById(R.id.surface_view);        
        mCubeRenderer = new CubeRenderer(this);
        mGlSurfaceView.setRenderer(mCubeRenderer);
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
    	sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
    }
    
    @Override
    protected void onStop() {
    	super.onStop();
    	sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
    	sensorManager.unregisterListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
    }

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		synchronized (this) {
			switch(event.sensor.getType()){
			case Sensor.TYPE_ACCELEROMETER:
				accelerometerOuputX.setText(Float.toString(event.values[0]));
				accelerometerOuputY.setText(Float.toString(event.values[1]));
				accelerometerOuputZ.setText(Float.toString(event.values[2]));
				break;
			case Sensor.TYPE_ORIENTATION:
				orientationX.setText(Float.toString(event.values[0]));
				orientationY.setText(Float.toString(event.values[1]));
				orientationZ.setText(Float.toString(event.values[2]));
				
				
				
				if(event.values[2] > 70){
					mCubeRenderer.setOrientation(event.values[2]);
				}
//				else
//					mCubeRenderer.setOrientation(false);
//				else if(event.values[2] < -70){
//					indicatorLeft.setVisibility(View.GONE);
//			        indicatorRight.setVisibility(View.VISIBLE);
//				}else{
//					indicatorLeft.setVisibility(View.GONE);
//			        indicatorRight.setVisibility(View.GONE);
//				}			
				
				break;
			}			
		}
	}
}